package com.example.design.pattern.behavior

/**
 *Create by Fanketly on 2022/9/1
 *Gitee:https://gitee.com/fanketly
 *desc:目的：主要用来解耦，客户只要把消息发到责任链上，无需关注请求细节和传递过程
优点：解耦，简化操作
缺点：性能会有一点影响，调试不太方便
 */
class RealInterceptor {
    val list = listOf(CacheInterceptor(), PostInterceptor())
    lateinit var mS: String
    fun main(string: String) {
        list.forEach {
            val chain = it.chain(string)
            if (chain is String)
                mS = string
        }
    }
}

interface Interceptor {
    fun chain(string: String): Any?
}

class CacheInterceptor : Interceptor {
    override fun chain(string: String): Boolean {
        print("cache $string")
        return true
    }
}

class PostInterceptor : Interceptor {
    override fun chain(string: String): String {
        print("进行了网络请求$string")
        return "请求成功"
    }
}